#!/usr/bin/env python

import graph
import math, time, sys
import gtk, gtk.glade, gtk.gdk, gobject, cairo

class Drawing:
    
    def __init__(self, drawable, margin):
        
        self.drawable = drawable
        self.margin = margin
    
    def startDrawing(self):
        
        self.cr = self.drawable.cairo_create()
    
        #rect = self.widget.get_allocation()
        w, h = 500, 500#rect.width, rect.height
        wi, hi = w - 2 * self.margin, h - 2 * self.margin

        self.cr.set_source_rgb(1.0, 1.0, 1.0)
        self.cr.rectangle(0, 0, w, h)
        self.cr.fill()
        self.cr.scale(wi, hi)
        self.cr.translate(self.margin / float(wi), self.margin / float(hi)) 

        self.cr.set_source_rgb(1.0, 1.0, 1.0)
        self.cr.rectangle(0, 0, 1, 1)
        self.cr.fill()

    def drawCircle(self, x, y, radius, color):
        self.cr.set_source_rgb(color[0], color[1], color[2])
        self.cr.arc(x, 1.0 - y, radius, 0, 2 * math.pi)
        self.cr.fill()            

    def drawLine(self, x1, y1, x2, y2, width, color):

        self.cr.set_source_rgb(color[0], color[1], color[2])
        self.cr.set_line_width(width)

        self.cr.move_to(x1, 1.0 - y1)
        self.cr.line_to(x2, 1.0 - y2)
        self.cr.stroke()

class App:

    def __init__(self):
        self.graph = graph.graph('dane', time.time())  

        self.interface = gtk.glade.XML('ui/ui.glade')
        self.canvas = self.interface.get_widget('canvas')
        self.interface.signal_autoconnect(self)

        self.width = 500 #TODO: get_allocation()
        self.height = 500

        self.gc = self.canvas.window.new_gc()
        
        self.buffer = gtk.gdk.Pixmap(self.canvas.window, self.width, self.height)

        self.drawing = Drawing(self.buffer, 40)
        self.timerID = gobject.idle_add(self.refresh)

        self.starttime = time.time()
        self.framecount = 0

        gtk.main()

    
    def handDestroy(self, widget, data=None):
        gtk.main_quit()

    def handExpose(self, widget, data=None):

        if self.graph.update(time.time()) or True:

            self.drawing.startDrawing()
            
            edges = self.graph.getEdges()
            for e in edges:
                sNode = e.startNode
                eNode = e.endNode
                self.drawing.drawLine(sNode.x, sNode.y, eNode.x, eNode.y, e.width, (0, 0, 0))
            
            nodes = self.graph.getNodes()
            for n in nodes:
                self.drawing.drawCircle(n.x, n.y, n.radius, n.color )
                

            cars = self.graph.getCars()
            for c in cars:
                x, y = c.getXY()
                self.drawing.drawCircle(x, y, c.radius, (1, 0, 0))

        self.canvas.window.draw_drawable(self.gc, self.buffer, 0, 0, 0, 0, -1, -1)
        self.framecount += 1
        if self.framecount == 50:
            tm = time.time()
            print 50 / (tm - self.starttime)
            self.starttime = tm
            self.framecount = 0
        
    def refresh(self):
        rect = gtk.gdk.Rectangle(0, 0, 500, 500)
        self.canvas.window.invalidate_rect(rect, True)
        self.canvas.window.process_updates(True)

        return True
            
            
        

if __name__ == '__main__':
    app = App()

